#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from generaldb.gbase8a.common.const import JsonConstant
from generaldb.gbase8a.util.gbase_util import GbaseUtil


class BaseParam:
    """

    """
    default_field = (
        JsonConstant.JOB,
        JsonConstant.SUB_JOB,
        JsonConstant.APPLICATION,
        JsonConstant.APP_ENV,
        JsonConstant.COPIES,
        JsonConstant.COPY,
        JsonConstant.JOB_ID,
        JsonConstant.JOB_PARAM,
        JsonConstant.EXTEND_INFO
    )

    def __init__(self, param_dict):
        self.param_dict = param_dict
        self.job = {}
        self.sub_job = {}
        self.application = {}
        self.app_env = {}
        self.copies = {}
        self.job_id = ""
        self.job_param = {}
        self.extend_info = {}
        self._parse_default_field(param_dict)

    def get(self, field):
        return self._get_field(field, self.param_dict)

    def _get_field(self, field, param_dict: dict = None):
        if not param_dict:
            param_dict = self.param_dict
        value = param_dict.setdefault(field, {})
        return value

    def _parse_default_field(self, param_dict):
        for field, value in param_dict.items():
            if field in self.default_field:
                attr = GbaseUtil.translate_to_snake_name(field)
                setattr(self, attr, value)
